package com.test.qrcode.manage.response;

/**
 * @program qrcode
 * @Title: 统一响应结构
 * @description: 使用REST框架实现前后端分离架构，我们需要首先确定返回的JSON响应结构是统一的，
 * 也就是说，每个REST请求将返回相同结构的JSON响应结构。不妨定义一个相对通用的JSON响应结构，其
 * 中包含两部分：元数据与返回值，其中，元数据表示操作是否成功与返回值消息等，返回值对应服务端方法所返回的数据。
 * @author: XueXiao
 * @create: 2019-02-28 10:04:01
 */
public class Response {
    private static final String OK = "0";
    private static final String ERROR = "error";
    private static final String SUCCESS = "交易成功";
    private static final String FAILURE = "交易失败";


    private Meta meta;     // 元数据
    private Object data;   // 响应内容

    public Response success() {
        this.meta = new Meta(OK, SUCCESS);
        return this;
    }

    public Response success(Object data) {
        this.meta = new Meta(OK, SUCCESS);
        this.data = data;
        return this;
    }

    public Response failure() {
        this.meta = new Meta(ERROR, FAILURE);
        return this;
    }

    public Response failure(String message) {
        this.meta = new Meta(ERROR, message);
        return this;
    }

    public Meta getMeta() {
        return meta;
    }

    public Object getData() {
        return data;
    }

    /** 
    * @Description: 请求元数据
    * @Author: XueXiao 
    * @Date: 2019/2/28 10:38
    */ 
    public class Meta {

        private String errorCde;
        private String errorMsg;

        public String getErrorCde() {
            return errorCde;
        }

        public void setErrorCode(String errorCde) {
            this.errorCde=errorCde;
        }

        public String getErrorMsg() {
            return errorMsg;
        }

        public void setErrorMsg(String errorMsg) {
            this.errorMsg=errorMsg;
        }

        public Meta (String errorCde, String errorMsg){
            this.errorCde = errorCde;
            this.errorMsg = errorMsg;
        }
    }
}
